// 回溯 DFS
class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;

    vector<vector<int>> permute(vector<int>& nums) {
        vector<bool> used(nums.size(), false);
        traceback(nums, used, 0, nums.size());
        return res;
    }

    void traceback(vector<int>& nums, vector<bool> used, int depth, int len) {
        if (depth == len) {
            res.push_back(path);
            return;
        }
        
        for (int i = 0; i < len; ++i) {
            if (used[i]) {
                continue;
            }
            path.push_back(nums[i]);
            used[i] = true;
            traceback(nums, used, depth + 1, len);
            used[i] = false;
            path.pop_back();
        }
    }
};